package kr.hkit.powerjava.work.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import kr.hkit.myframework.db.DbUtil;
import kr.hkit.powerjava.work.dto.Product;

public class ProductDAO implements DaoInterface<Product> {

	private Connection connection;
	private PreparedStatement pstmt;
	
	public ProductDAO(Connection connection){
		this.connection = connection;
	}
	
	@Override
	public void insertItem(Product item) throws SQLException {
		String sql = "insert into product_table values(?,?,?,?,?)";
		pstmt = connection.prepareStatement(sql);
		
		pstmt.setString(1, item.getProCode());
		pstmt.setString(2, item.getProName());
		pstmt.setInt(3, item.getProPrice());
		pstmt.setInt(4, item.getProCount());
		pstmt.setInt(5, item.getProMargin());
		pstmt.executeUpdate();
		DbUtil.close(pstmt);
	}
	

	@Override
	public Product searchItem(Product item) throws SQLException {
		Product product = null;
		String sql = "select pro_code, pro_name, pro_price, pro_count, pro_margin from product_table where pro_code=?";
		pstmt = connection.prepareStatement(sql);
		pstmt.setString(1, item.getProCode());
		ResultSet rs = pstmt.executeQuery();
		
		if(rs.next()){
			product = new Product();
			product.setProCode(rs.getString(1));
			product.setProName(rs.getString(2));
			product.setProPrice(rs.getInt(3));
			product.setProCount(rs.getInt(4));
			product.setProMargin(rs.getInt(5));
		}
		return product;
	}

	/*@Override
	public ArrayList<Product> listItems1() throws SQLException {
		ArrayList<Product> list = new ArrayList<>();
		String sql = "select (select count(*)+1 from product_table p2 where p1.pro_count < p2.pro_count)as rank,pro_code, pro_name, pro_price, pro_count, price-buga as gong, price/11 as buga,pro_price*pro_count as price,pro_margin, gong*pro_margin as marginMoney from product_table p1;"
				 
		return null;
	}

	@Override
	public ArrayList<Product> listItems2() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}*/

}
